package de.gwtprime.client.widgets.util;

import gwtupload.client.IUploadStatus.Status;
import gwtupload.client.IUploader;
import gwtupload.client.IUploader.UploadedInfo;
import gwtupload.client.MultiUploader;
import gwtupload.client.PreloadedImage;
import gwtupload.client.PreloadedImage.OnLoadPreloadedImageHandler;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.event.ApplyTabHeightEvent;
import de.gwtprime.client.util.AppUtils;
import de.gwtprime.client.util.HasTextTitleAndProxy;

public class UploadWidget extends Composite {

	private static UploadWidgetUiBinder uiBinder = GWT
			.create(UploadWidgetUiBinder.class);
	
	@UiField Button showUploadWidget;
	@UiField FlowPanel panelImages;
	@UiField SimplePanel uploadBtn;
	@UiField Label uploadTitle;
	@UiField Label previewImagesTitle;
	
	private String uploadWidgetButtonText = "Image upload";
	interface UploadWidgetUiBinder extends UiBinder<Widget, UploadWidget> {}
	
	
	private ClientFactory clientFactory;
	private HasTextTitleAndProxy hasTextElement;

	public UploadWidget(final ClientFactory clientFactory, HasTextTitleAndProxy hasTextElement) {
		initWidget(uiBinder.createAndBindUi(this));
		
		this.clientFactory = clientFactory;
		this.hasTextElement = hasTextElement;
		
		uploadTitle.setText("Upload images:");
		previewImagesTitle.setText("Image preview");
		
		showUploadWidget.setText(uploadWidgetButtonText);
		
		panelImages.setVisible(false);
		uploadBtn.setVisible(false);
		
		uploadTitle.setVisible(false);
		previewImagesTitle.setVisible(false);
		
		// Create a new uploader panel and attach it to the document
		MultiUploader defaultUploader = new MultiUploader();
		
		uploadBtn.clear();
		uploadBtn.add(defaultUploader);

		// Add a finish handler which will load the image once the upload finishes
		defaultUploader.addOnFinishUploadHandler(onFinishUploaderHandler);
		
		showUploadWidget.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				
				boolean visible = panelImages.isVisible();
				panelImages.setVisible(!visible);
				uploadBtn.setVisible(!visible);
				uploadTitle.setVisible(!visible);
				previewImagesTitle.setVisible(!visible);
				showUploadWidget.setText(visible ? uploadWidgetButtonText : "Hide image upload");
				clientFactory.getEventBus().fireEvent(new ApplyTabHeightEvent());
			}
		});
		
	}
	
	
	private String url;
	// Load the image in the document and in the case of success attach it to the viewer
	private IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
		public void onFinish(IUploader uploader) {
			if (uploader.getStatus() == Status.SUCCESS) {

				new PreloadedImage(uploader.fileUrl(), showImage);

				//editWidget.setText(editWidget.getText()+"<img src=\""+uploader.fileUrl()+"\">");
				// The server sends useful information to the client by default
				UploadedInfo info = uploader.getServerInfo();

				url = info.message;
			}
		}
	};

	// Attach an image to the pictures viewer
	private OnLoadPreloadedImageHandler showImage = new OnLoadPreloadedImageHandler() {
		public void onLoad(final PreloadedImage image) {

			int computedHeight;
			int maxWidth = 600;
			if(image.getWidth()>=image.getHeight()){
				if(image.getWidth()<=maxWidth){
					computedHeight = image.getHeight();
					url+="=s0";
				}else{
					url+="=s600";
					computedHeight = image.getHeight()*maxWidth/image.getWidth();
				}
			}else{
				if(image.getWidth()<=maxWidth){
					url+="=s0";
					computedHeight = image.getHeight();
				}else{
					url+="=s600";
					computedHeight = 600;
				}
			}
			
			Image im = new Image(url);
			im.getHeight();
			final String images =  AppUtils.addImage(hasTextElement.getImages(), url, computedHeight);
		
			hasTextElement.setImages(images);
			
			image.setWidth("75px");
			panelImages.add(image);
			hasTextElement.updateTextAndImages(images, getText()+"["+AppUtils.getLastIndexOfImage(images)+"]");
		}
	};
	
	
	public String getText(){
		return hasTextElement.getText();
	}
	
	





}
